package com.tjetc.receiver;

import com.tjetc.constant.MessageConstant;
import com.tjetc.domain.BClassify;
import com.tjetc.mapper.BClassifyMapper;
import com.tjetc.repository.BClassifyRepository;
import com.tjetc.vo.OperationMessage;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

import static com.tjetc.constant.CodeConstant.*;
import static com.tjetc.constant.RedisConstant.*;

/**
 * @author zc
 * @Date 2025/7/16 下午9:27
 */
@Component
public class BClassifyReceiver {
    @Autowired
    private BClassifyMapper bClassifyMapper;
    @Autowired
    private BClassifyRepository bClassifyRepository;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @RabbitListener(queues = "bClassifyQueue")
    public void processBClassify(OperationMessage msg) {
        HashOperations<String, Object, Object> ops = stringRedisTemplate.opsForHash();
        //拿出类型
        String type = msg.getType();
        if (type.equals(MessageConstant.IMPORT_ALL)) {
            try {
                List<BClassify> list = bClassifyMapper.findAll();
                System.out.println(list);
                for (BClassify bClassify : list) {
                    bClassifyRepository.save(bClassify);
                }
                ops.put(OPERATION_KEY + msg.getOperationId(), DESC, "导入成功");
                ops.put(OPERATION_KEY + msg.getOperationId(), STATE, SUCCESS.toString());
            } catch (Exception e) {
                e.printStackTrace();
                ops.put(OPERATION_KEY + msg.getOperationId(), DESC, "导入失败");
                ops.put(OPERATION_KEY + msg.getOperationId(), STATE, FAIL.toString());
            }
        }else if (type.equals(MessageConstant.DELETE_ALL)){
            try {
               bClassifyRepository.deleteAll();
                ops.put(OPERATION_KEY + msg.getOperationId(), DESC, "删除成功");
                ops.put(OPERATION_KEY + msg.getOperationId(), STATE, SUCCESS.toString());
            } catch (Exception e) {
                e.printStackTrace();
                ops.put(OPERATION_KEY + msg.getOperationId(), DESC, "删除失败");
                ops.put(OPERATION_KEY + msg.getOperationId(), STATE, FAIL.toString());
            }
        }

//        }else if (type.equals("add")){
//            System.out.println("add");
//            int rows =  productService.add((Product) map.get("product"));
//        }else if (type.equals("update")){
//            System.out.println("update");
//            int rows =  productService.update((Product) map.get("product"));
//        }else if (type.equals("del")){
//            System.out.println("delete");
//            int rows = productService.del((Integer)map.get("id"));
//        }
    }
}
